package com.flymobi.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.flymobi.fw.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * 
 * Title:
 * 
 * Description:
 * 
 * Copyright: Copyright (c) 2008
 * 
 * @author haoxz11
 * @created Mar 7, 2008 11:20:02 AM
 * @version $Id: WebContext.java,v 1.1 2010/04/09 03:27:07 airsou Exp $
 */
public class WebContext {
	private ActionContext context;
	private CookieUtil cookieUtil;
	private HttpServletRequest request;
	private HttpServletResponse response;

	public WebContext(ActionContext context) {
		this.context = context;
		this.request = (HttpServletRequest) context
				.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
		this.response = (HttpServletResponse) context
				.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse");
		cookieUtil = new CookieUtil(request, response);
	}

	public String getUrl() {
		String query = request.getQueryString();
		String tmp = "";
		if (StringUtil.isEmpty(query)) {
			tmp = request.getRequestURI();
		} else {
			tmp = request.getRequestURI() + "?" + request.getQueryString();
		}
		return tmp;
	}

	public Object put(String key, Object value) {
		context.put(key, value);
		return value;
	}

	public Object get(String key) {
		if (StringUtils.isEmpty(key)) {
			return null;
		}
		return context.get(key);
	}

	/**
	 * 
	 * 
	 * @param context
	 *            Description of the Parameter
	 * @param key
	 *            Description of the Parameter
	 * 
	 * @return The clientValue value
	 */
	public String getClientValue(String key) {
		return cookieUtil.get(key,"-1");
	}


	/**
	 * 
	 * 
	 * @param context
	 *            The new clientValue value
	 * @param key
	 *            The new clientValue value
	 * @param value
	 *            The new clientValue value
	 */
	public void setClientValue(String key, String value) {
		cookieUtil.add(key, value,-1,null);
	}

	public Object[] getKeys() {
		return null;
	}

	public boolean containsKey(Object a) {
		return false;
	}

	public Object remove(Object key) {
		return null;
	}

	public ActionContext getContext() {
		return context;
	}

	public void setContext(ActionContext context) {
		this.context = context;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

}